#include "LinkQueue.h"
#include "LinkStack.h"
#include <string>

struct Item
{
    Item() {}
    Item(const std::string &name, int id, double price) : name(name), id(id), price(price) {}
    int id;
    std::string name;
    double price;
};

class Shopping
{
public:
    Shopping() : size(0) {}
    Shopping(const int &size) : size(size) {}
    void Push();
    void deletePop();
    void display();
    void enqueue();
    void dequeue();
    void shoppingHistory();
    

private:
    LinkStack basket;
    LinkQueque pay;
    LinkQueque history;
    const int size;
};

void Shopping::Push()
{
    if (basket.Size() == size)
    {
        std::cout << "购物车已满!\n";
    }
    else
    {
        Item *goods = new Item;
        std::cout << "请输入商品的编号";
        std::cin >> goods->id;
        std::cout << "请输入商品的名称";
        std::cin >> goods->name;
        std::cout << "请输入商品的价格";
        std::cin >> goods->price;

        basket.Push(goods);
        std::cout << goods->name << "已添加进购物车" << std::endl;
    }
}

void Shopping::deletePop()
{
    basket.Pop();
    std::cout << "上次输入的商品已经删除";
}

void Shopping::display()
{
    if (basket.Size() == 0)
    {
        std::cout << "购物车为空!\n";
    }
    else
        std::cout << "购物车商品:\n";
    LinkStack temp;
    while (!basket.IsEmpty())
    {
        temp.Push(*basket.GetTop());
        basket.Pop();
    }

    while (!temp.IsEmpty())
    {
        Item *t = (Item *)*temp.GetTop();
        std::cout << "商品编号:" << t->id << "  商品名称:" << t->name << "  商品价格:" << t->price << std::endl;
        basket.Push(t);
        temp.Pop();
    }
}

void Shopping::enqueue()
{
    while (!basket.IsEmpty())
    {
        pay.Push(*basket.GetTop());
        basket.Pop();
    }
}

void Shopping::dequeue()
{
    int money = 0;
    while (!pay.IsEmpty())
    {
        Item *s = (Item *)*pay.GetFront();
        money += s->price;
        history.Push(s);
        pay.Pop();
    }
    std::cout << "总计" << money << "元" << std::endl;
}

void Shopping::shoppingHistory()
{
    int money = 0;
    LinkQueque temp;
    if (history.IsEmpty())
        std::cout << "暂未有历史记录!\n";
    else
        std::cout << "历史购物车:\n";
    while (!history.IsEmpty())
    {
        temp.Push(*history.GetFront());
        history.Pop();
    }

    while (!temp.IsEmpty())
    {
        Item *s = (Item *)*temp.GetFront();
        money += s->price;
        std::cout << " 商品编号:" << s->id << "  商品名称:" << s->name << "  商品价格:" << s->price << std::endl;
        history.Push(s);
        temp.Pop();
    }
    std::cout << "总消费:" << money << "元" << std::endl;
}

int main()
{
    Shopping c(20);

    while (1)
    {
        int Ops = -1;
        std::cout << "请输入你要进行的操作:" << std::endl;
        std::cout << "1、添加商品\n"
                  << "2、删除上一次添加\n"
                  << "3、查看购物车\n"
                  << "4、结算商品\n"
                  << "5、查看历史购买记录\n"
                  << "0、退出" << std::endl;
        std::cin >> Ops;
        switch (Ops)
        {
        case 1:
            c.Push();
            break;
        case 2:
            c.deletePop();
            break;
        case 3:
            c.display();
            break;
        case 4:
            c.enqueue();
            c.dequeue();
            break;
        case 5:
            c.shoppingHistory();
            break;
        case 0:
            exit(-1);
        default:
            break;
        }
    }
    return 0;
}
